layui.define(['jquery', 'element', 'mMenu', 'mTab', 'mTheme'], function(exports) {
	var $ = layui.$,
		layer = layui.layer,
		mMenu = layui.mMenu,
		element = layui.element,
		mTab = layui.mTab,
		mTheme = layui.mTheme;

	if (!/http(s*):\/\//.test(location.href)) {
		var tips = "请先将项目部署至web容器（Apache/Tomcat/Nginx/IIS/等），否则部分数据将无法显示";
		return layer.alert(tips);
	}

	var mAdmin = {

		/**
		 * 后台框架初始化
		 * @param options.initUrl   后台初始化接口地址
		 * @param options.clearUrl   后台清理缓存接口
		 * @param options.urlHashLocation URL地址hash定位
		 * @param options.skin 默认皮肤
		 * @param options.multiModule 是否开启多模块
		 * @param options.menuChildOpen 是否展开子菜单
		 * @param options.loadingTime 初始化加载时间
		 * @param options.pageAnim iframe窗口动画
		 * @param options.maxTabNum 最大的tab打开数量
		 */
		render: function(options) {
			options.initUrl = options.initUrl || null;
			options.urlHashLocation = options.urlHashLocation || false;
			options.skin = options.skin || 0;
			options.multiModule = options.multiModule || false;
			options.menuChildOpen = options.menuChildOpen || false;
			options.loadingTime = options.loadingTime || 1;
			options.pageAnim = options.pageAnim || false;
			options.maxTabNum = options.maxTabNum || 20;
			lianyi.request({
				url: options.initUrl,
				msg: null,
				success: function(res) {
					if (res == null)
						return lianyi.fail('暂无菜单信息')

					window.admin = res.admin;
					if (admin.avatar) $('.admin-admin > a > img').attr('src', admin.avatar);
					$('.admin-admin > a > .admin-name').text(admin.name || admin.user);

					var data = res.data;
					mAdmin.renderLogo(data.logo);
					mAdmin.renderClear(data.clear);
					mAdmin.renderHome(data.home);
					mAdmin.renderAnim(options.pageAnim);
					mAdmin.listen();
					mMenu.render({
						menuList: data.menu,
						multiModule: options.multiModule,
						menuChildOpen: options.menuChildOpen
					});
					mTab.render({
						filter: 'adminTab',
						urlHashLocation: options.urlHashLocation,
						multiModule: options.multiModule,
						menuChildOpen: options.menuChildOpen,
						maxTabNum: options.maxTabNum,
						menuList: data.menu,
						homeInfo: data.home,
						listenSwichCallback: function() {
							mAdmin.renderDevice();
						}
					});
					mTheme.render({
						skin: options.skin,
						listen: true,
					});
					mAdmin.deleteLoader(options.loadingTime);
				}
			});
		},

		/**
		 * 初始化logo
		 * @param data
		 */
		renderLogo: function(data) {
			var html = '<div><img src="' + data.image + '" alt="logo"><h1>' + data.title + '</h1></div>';
			$('.admin-logo').html(html).attr('href', data.href);
		},

		/**
		 * 初始化首页
		 * @param data
		 */
		renderHome: function(data) {
			sessionStorage.setItem('adminHomeHref', data.href);
			$('#adminHomeTabId').html('<span class="admin-tab-active"></span><span unselectable="on" class="disable-close noselect">' + data.title + '</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>');
			$('#adminHomeTabId').attr('lay-id', data.href);
			$('#adminHomeTabIframe').html('<iframe width="100%" height="100%" frameborder="no" border="0" marginwidth="0" marginheight="0"  src="' + data.href + '"></iframe>');
		},

		/**
		 * 初始化缓存地址
		 * @param clearUrl
		 */
		renderClear: function(clearUrl) {
			$('.admin-clear').attr('data-href', clearUrl);
		},

		/**
		 * 初始化iframe窗口动画
		 * @param anim
		 */
		renderAnim: function(anim) {
			if (anim) {
				$('#admin-page-anim', 'head').remove();
				$('head').append('<style type="text/css" id="admin-page-anim">' +
					'.layui-tab-item.layui-show {animation:moveTop 1s;-webkit-animation:moveTop 1s;animation-fill-mode:both;-webkit-animation-fill-mode:both;position:relative;height:100%;-webkit-overflow-scrolling:touch;}\n' +
					'@keyframes moveTop {0% {opacity:0;-webkit-transform:translateY(30px);-ms-transform:translateY(30px);transform:translateY(30px);}\n' +
					'    100% {opacity:1;-webkit-transform:translateY(0);-ms-transform:translateY(0);transform:translateY(0);}\n' +
					'}\n' +
					'@-o-keyframes moveTop {0% {opacity:0;-webkit-transform:translateY(30px);-ms-transform:translateY(30px);transform:translateY(30px);}\n' +
					'    100% {opacity:1;-webkit-transform:translateY(0);-ms-transform:translateY(0);transform:translateY(0);}\n' +
					'}\n' +
					'@-moz-keyframes moveTop {0% {opacity:0;-webkit-transform:translateY(30px);-ms-transform:translateY(30px);transform:translateY(30px);}\n' +
					'    100% {opacity:1;-webkit-transform:translateY(0);-ms-transform:translateY(0);transform:translateY(0);}\n' +
					'}\n' +
					'@-webkit-keyframes moveTop {0% {opacity:0;-webkit-transform:translateY(30px);-ms-transform:translateY(30px);transform:translateY(30px);}\n' +
					'    100% {opacity:1;-webkit-transform:translateY(0);-ms-transform:translateY(0);transform:translateY(0);}\n' +
					'}' +
					'</style>');
			}
		},

		/**
		 * 初始化设备端
		 */
		renderDevice: function() {
			if (mAdmin.checkMobile()) {
				$('.admin-tool i').attr('data-side-fold', 1);
				$('.admin-tool i').attr('class', 'fa fa-outdent');
				$('body').removeClass('admin-mini');
				$('body').addClass('admin-all');
			}
		},

		/**
		 * 初始化加载时间
		 * @param loadingTime
		 */
		deleteLoader: function(loadingTime) {
			setTimeout(function() {
				$('.admin-loader').fadeOut();
			}, loadingTime * 1000)
		},

		/**
		 * 判断是否为手机
		 * @returns {boolean}
		 */
		checkMobile: function() {
			var ua = navigator.userAgent.toLocaleLowerCase();
			var pf = navigator.platform.toLocaleLowerCase();
			var isAndroid = (/android/i).test(ua) || ((/iPhone|iPod|iPad/i).test(ua) && (/linux/i).test(pf)) ||
				(/ucweb.*linux/i.test(ua));
			var isIOS = (/iPhone|iPod|iPad/i).test(ua) && !isAndroid;
			var isWinPhone = (/Windows Phone|ZuneWP7/i).test(ua);
			var clientWidth = document.documentElement.clientWidth;
			if (!isAndroid && !isIOS && !isWinPhone && clientWidth > 1024) {
				return false;
			} else {
				return true;
			}
		},

		/**
		 * 监听
		 */
		listen: function() {
			/**
			 * 清理
			 */
			$('body').on('click', '[data-clear]', function() {
				sessionStorage.clear();

				// 判断是否清理服务端
				var clearUrl = $(this).attr('data-href');
				if (clearUrl != undefined && clearUrl != '' && clearUrl != null) {
					lianyi.request({ url: clearUrl });
				}
			});

			/**
			 * 刷新
			 */
			$('body').on('click', '[data-refresh]', function() {
				$(".layui-tab-item.layui-show").find("iframe")[0].contentWindow.location.reload();
				lianyi.success('刷新成功');
			});

			/**
			 * 监听提示信息
			 */
			var inMenu = inTips = false,
				menuTimer;

			function openMenuTips(elem) {
				if (mAdmin.checkMobile()) {
					return false;
				}
				var classInfo = elem.parent().attr('class'),
					tips = elem.parent().prop("innerHTML"),
					grade = elem.attr('grade') || 0,
					isShow = $('.admin-tool i').attr('data-side-fold');
				if (isShow == 0 && tips) {
					tips = "<ul class='layui-nav layui-nav-tree layui-this'><li class='layui-nav-item'>" + tips + "</li></ul>";
					return layer.tips(tips, elem.parent(), {
						tips: [
							2,
							'#2f4056'
						],
						time: 0,
						skin: "left-menu-tips",
						success: function(el) {
							var elem2 = $(el).find('.layui-nav > .layui-nav-item:has(.layui-nav-child)');
							if (elem2 && elem2.length > 0) {
								elem2.addClass('layui-nav-itemed').children('.layui-nav-child').show();
								elem2.find('a[grade]').attr('grade', function() {
									return parseInt($(this).attr('grade')) - parseInt(grade);
								});
							}
							var left = $(el).position().left - 10;
							$(el).css('left', left);
							element.render();
						}
					});
				}
			}

			function closeMenuTips() {
				if (mAdmin.checkMobile()) {
					return false;
				}
				var isShow = $('.admin-tool i').attr('data-side-fold');
				if (isShow == 0) {
					try {
						$('.admin-mini .admin-side .layui-nav-tree a[data-menu-tips]').removeAttr('data-menu-tips');
						layer.close(window.menuTips);
					} catch (e) {
						console.log(e.message);
					}
				}
			}

			$(document).on('mouseenter', '.admin-mini .admin-side .layui-nav-tree a', function() {
				clearTimeout(menuTimer);
				if ($('.left-menu-tips').length < 1 || $(this).attr('data-menu-tips') != $('.left-menu-tips').attr('times')) {
					closeMenuTips();
					inMenu = true;
					$(this).attr('data-menu-tips', window.menuTips = openMenuTips($(this)));
				}
			});
			$(document).on('mouseleave', '.admin-mini .admin-side .layui-nav-tree a', function() {
				inMenu = false;
				menuTimer = setTimeout(function() {
					if (!inTips) closeMenuTips();
				}, 10);
			});
			$(document).on('mouseenter', '.left-menu-tips', function() {
				inTips = true;
			});
			$(document).on('mouseleave', '.left-menu-tips', function() {
				inTips = false;
				menuTimer = setTimeout(function() {
					if (!inMenu) closeMenuTips();
				}, 10);
			});

			/**
			 * 点击遮罩层
			 */
			$('body').on('click', '.admin-make', function() {
				mAdmin.renderDevice();
			});
		}
	};

	exports("mAdmin", mAdmin);
});